package com.bridge.applets.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bridge.applets.mapper.BridgeRecordMapper;
import com.bridge.applets.model.entity.BridgeRecord;
import com.bridge.applets.model.vo.req.BridgeRecodeListReq;
import com.bridge.applets.service.BridgeRecordService;
import com.bridge.common.enums.YesOrNoEnum;
import com.bridge.common.model.param.BasePageParam;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xyjian
 * @since 2024-11-06
 */
@Service
public class BridgeRecordServiceImpl extends ServiceImpl<BridgeRecordMapper, BridgeRecord> implements BridgeRecordService {

    @Override
    public BridgeRecord queryById(Long recordId) {
        if(Objects.isNull(recordId)){
            return null;
        }
        return this.lambdaQuery()
                .eq(BridgeRecord::getId,recordId)
                .eq(BridgeRecord::getIsDeleted, YesOrNoEnum.NO.getType())
                .one();
    }

    @Override
    public Page<BridgeRecord> queryPage(BridgeRecodeListReq param) {
        Page<BridgeRecord> page=new Page<>(param.getPageNum(),param.getPageSize());
       return this.page(page, new LambdaQueryWrapper<BridgeRecord>()
                .eq(BridgeRecord::getIsDeleted, YesOrNoEnum.NO.getType())
                .orderByDesc(BridgeRecord::getId)
        );
    }

    @Override
    public Integer countNum(Long firstRecordId) {
        return this.baseMapper.countNum(firstRecordId);
    }

    @Override
    public List<BridgeRecord> page(Long firstRecordId, Long offset, Long pageSize) {
        return this.baseMapper.page(firstRecordId,offset,pageSize);
    }

    @Override
    public BridgeRecord queryByCode(String bridgeCode) {
        return this.lambdaQuery()
                .eq(BridgeRecord::getBridgeCode,bridgeCode)
                .eq(BridgeRecord::getIsDeleted, YesOrNoEnum.NO.getType())
                .last(" limit 1 ")
                .one();
    }

    @Override
    public void deleteById(Long id) {
        this.lambdaUpdate()
                .eq(BridgeRecord::getId,id)
                .set(BridgeRecord::getIsDeleted,YesOrNoEnum.YES.getType())
                .update();
    }
}
